var currentTreeNode = null;
var treeObj;
var minForm;


$(document).ready(function () {
    $("#edit_node").css("display", "none");
    treeObj = mini.get("tree");
    minForm = new mini.Form("#form1");
    initTree();
});


/***
 * 加载树
 * */
function initTree() {
    Tree.getTree(treeObj, "sysAttachMenu!list.do", "attachMenuId", "parentId");
    getTreeSelect(mini.get("treeSelect"), "sysAttachMenu!list.do", {}, "attachMenuId", "parentId");
}

function refershTree() {
    initTree();
    $("#edit_node").css("display", "none");
    $("#sBtn").css("display", "none");
}

/***
 * 新增根节点
 * */
function newroot() {
    var data = {
        'sysAttachMenu.menuName': 'newRoot',
        'sysAttachMenu.menuSort': '0'
    };
    Tree.addTreeRoot("sysAttachMenu!add.do", data, treeObj, "refershTree");
}

/***
 * 新增同级节点
 * */
function newcur() {
    var parentnode = treeObj.getParentNode(currentTreeNode);
    if (parentnode.attachMenuId) {
        parentID = parentnode.attachMenuId;
    } else {
        parentID = '0';
    }
    var data = {
        'sysAttachMenu.menuName': 'newNode',
        'sysAttachMenu.menuSort': '0',
        'sysAttachMenu.parentId': parentID
    };
    Tree.addTreeCurrent("sysAttachMenu!add.do", data, treeObj, "refershTree");
}

/***
 * 新增子节点
 * */
function newchild() {
    if (currentTreeNode) {
        parentID = currentTreeNode.attachMenuId;
    } else {
        parentID = '0';
    }
    var data = {
        'sysAttachMenu.menuName': 'newNode',
        'sysAttachMenu.menuSort': '0',
        'sysAttachMenu.parentId': parentID
    };
    Tree.addTreeCurrent("sysAttachMenu!add.do", data, treeObj, "refershTree");
}

function treeselected(e) {
    var tree = e.sender;
    var node = e.node;
    currentTreeNode = node;
    console.log(currentTreeNode)
    enableOpe(true);
    $("#curopename").html("编辑当前节点");
    mini.get("attachMenuId").setValue(node.attachMenuId);
    mini.get("menuName").setValue(node.menuName);
    mini.get("menuSort").setValue(node.menuSort);
    mini.get("menuRemark").setValue(node.menuRemark);
    mini.get("parentId").setValue(node.parentId);
    var pnode = tree.getParentNode(currentTreeNode);
    if (pnode.codeName) {
        mini.get("treeSelect").setText(pnode.menuName);
    } else {
        mini.get("treeSelect").setText("没有父目录");
    }
    $("#edit_node").css("display", "block");
    $("#sBtn").css("display", "block");
}

/***
 * 保存数据
 * */
function saveData() {
    if (minForm.isValid() == false) {
        mini.alert("验证失败，点击页面红色叹号查看！");
        return;
    }
    var formData = $("#form1").serializeJSON();
    ActHttpRequest("sysAttachMenu!update.do", formData, function (data) {
        if (data.OK == "OK") {
            treeObj.selectNode(currentTreeNode);
            refershTree();
            currentTreeNode = treeObj.getSelectedNode();
            var pnode = treeObj.getParentNode(currentTreeNode);
            while (pnode[treeObj.idField]) {
                treeObj.expandNode(pnode);
                pnode = treeObj.getParentNode(pnode);
            }
            var e = {};
            e.sender = treeObj;
            e.node = currentTreeNode;
            treeselected(e);
        } else {
            mini.alert(data.message);
        }
    }, null, null, null, '保存中。。。', true);
}

function delNode() {
    mini.confirm("您确定要删除'" + currentTreeNode.codeName + "'吗？", "删除确认", function (action) {
        if (action != "ok") {
            return;
        }
        ActHttpRequest("sysAttachMenu!del.do?id=" + currentTreeNode.attachMenuId, {}, function (data) {
            if (data.OK == "OK") {
                treeObj.removeNode(currentTreeNode);
                enableOpe(false);
                $("#deletefile_item").get(0).src = "../../images/button/un_delete.gif";
                $("#deletefile_item").get(0).onclick = null;//删除目录
                $("#edit_node").css("display", "none");
                $("#sBtn").css("display", "none");
            } else {
                mini.alert(data.message);
            }
        }, null, null, null, '删除中。。。', true);
    });
}

function enableOpe(f) {
    if (f) {

        $("#deletefile_item").get(0).src = "../../images/button/delete.gif";
        $("#deletefile_item").get(0).onclick = delNode;//删除目录

        $("#newcurnode").get(0).src = "../../images/button/addbrother.gif";
        $("#newcurnode").get(0).onclick = newcur//新增当前目录

        $("#newchildnode").get(0).src = "../../images/button/addchild.gif";
        $("#newchildnode").get(0).onclick = newchild//新增子目录

    } else {
        $("#newchildnode").get(0).src = "../../images/button/un_addchild.gif";
        $("#newchildnode").get(0).onclick = null//新增子目录
        $("#newcurnode").get(0).src = "../../images/button/un_addbrother.gif";
        $("#newcurnode").get(0).onclick = null//新增子目录
    }
}